/*
 * string_util.h
 *
 *  Created on: 2008-9-26
 *      Author: fanliangliang
 */

#ifndef TEST_STRING_UTIL_H_
#define TEST_STRING_UTIL_H_

#include "toxic.h"
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>

using namespace std;
using namespace toxic;

inline void test_starts_with(const string& s, const string& prefix, size_t pos = 0) {
	cout << '"' << s << "\" \"" << prefix << "\" ";
	if (pos != 0) {
		cout << pos << " ";
	}
	cout << toxic::startsWith(s, prefix, pos) << endl;
}

inline void test_ends_with(const string& s, const string& suffix) {
	cout << '"' << s << "\" \"" << suffix << "\" " << toxic::endsWith(s, suffix) << endl;
}


void do_test_string_util() {

	cout << "***** string_util *****" << endl;

	string str("  ##2008/08/11 10:48:07  ##");

	cout << "original string:" << str << endl;

	// Test replace
	replace(str, '/', '-');

	// str = "  ##2008-08-11 10:48:07  ##"
	cout << "replaced string:" << str << endl;

	// Test trim
	trim(str, " #");

	// str = "2008-08-11 10:48:07"
	cout << "trimed string:" << str << endl;

	// Test split
	vector<string> result;
	size_t n = split(result, str, " -:");

	cout << "split result size:" << n << endl;
	cout << "split result:";
	foreach (const string& s, result) {
		cout << s << ",";
	}
	cout << endl;

	// Test ltrim
	cout << "split result(ltrim):";
	foreach (string& s, result) {
		cout << ltrim(s, "0") << ",";
	}
	cout << endl;

	// Test rtrim
	cout << "split result(rtrim):";
	foreach (string& s, result) {
		cout << rtrim(s, "8") << ",";
	}
	cout << endl;

	// Test toLowerCase
	cout << "test toLowerCase:" << boolalpha
		<< (toLowerCase("ABCd") == "abcd") << noboolalpha << endl;

	// Test toUpperCase
	cout << "test toUpperCase:" << boolalpha
		<< (toUpperCase("AbCd") == "ABCD") << noboolalpha << endl;

	// Test startsWith
	cout << boolalpha;
	cout << "test startsWith:" << endl;
	test_starts_with("", "");
	test_starts_with("", "abc");
	test_starts_with("abc", "");
	test_starts_with("abc", "abc");
	test_starts_with("abcdefg", "abc");
	test_starts_with("abcdefg", "cd", 2);
	test_starts_with("abcdefg", "bc");
	test_starts_with("abcdefg", "xyz");

	// Test endsWith
	cout << "test endsWith:" << endl;
	test_ends_with("", "");
	test_ends_with("", "abc");
	test_ends_with("abc", "");
	test_ends_with("abc", "abc");
	test_ends_with("abcdefg", "efg");
	test_ends_with("abcdefg", "ef");
	test_ends_with("abcdefg", "xyz");
	cout << noboolalpha;

	// Output:
	// original string:  ##2008/08/11 10:48:07  ##
	// replaced string:  ##2008-08-11 10:48:07  ##
	// trimed string:2008-08-11 10:48:07
	// split result size:6
	// split result:2008,08,11,10,48,07,
	// split result(ltrim):2008,8,11,10,48,7,
	// split result(rtrim):200,,11,10,4,7,
	// test toLowerCase:true
	// test toUpperCase:true
	// test startsWith:
	// "" "" true
	// "" "abc" false
	// "abc" "" true
	// "abc" "abc" true
	// "abcdefg" "abc" true
	// "abcdefg" "cd" 2 true
	// "abcdefg" "bc" false
	// "abcdefg" "xyz" false
	// test endsWith:
	// "" "" true
	// "" "abc" false
	// "abc" "" true
	// "abc" "abc" true
	// "abcdefg" "efg" true
	// "abcdefg" "ef" false
	// "abcdefg" "xyz" false
}

#endif /* TEST_STRING_UTIL_H_ */
